<template>
	<view class="login">
		<view class="title">欢迎进入凡人修仙传</view>
		<view class="user-info">
			<input type="text" v-model="user"/>
			<input type="password" v-model="pass"/>
		</view>
		<view>
			<button @tap="login" v-show="judge">注册</button>
			<button @tap="deng">登陆</button>
		</view>
		<input v-model="text" type="text" />
		<button @tap="info">发送</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: "",
				pass: "",
				judge: true,
				text: ""
			}
		},
		methods: {
			info () {
				this.$socket.emit('info',{type: this.text, msg: 'hello ya'})
			},
			login () {
				let self = this
				let reg = /[\u4e00-\u9fa5]+/g
				if (reg.test(this.user) || reg.test(this.pass)) {
					self.user = ""
					self.pass = ""
					uni.showToast({
						title: '账号或密码不能包含中文',
						duration: 1000
					})
					return 
				}
				uni.request({
					url: 'http://25q686n876.qicp.vip/create',
					data: {
						user: this.user,
						pass: this.pass
					},
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					method: 'POST',
					success: function (data) {
						console.log(data)
						if (data.data.ok === 200) {
							self.judge = false
							self.user = ""
							self.pass = ""
							uni.showToast({
								title: '注册成功,请登陆',
								duration: 1000
							})
						}
					}
				})
			},
			deng () {
				let user = this.user
				let pass = this.pass
				uni.request({
					url: "http://25q686n876.qicp.vip/deng",
					method: "POST",
					data: {
						id: user,
						name: pass
					},
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					success: function (data) {
						console.log(data)
						if (data.data.ok === 200) {
							uni.showLoading({
								title: '登陆成功',
								success: function () {
									setTimeout(()=>{
										uni.hideLoading()
										uni.navigateTo({
											url:'/pages/info/info'
										})
									},1500)
								}
							})
						}
						if (data.data.ok === 404) {
							uni.showToast({
								title:'账号密码错误'
							})
						}
						if (data.data.ok === 500) {
							uni.showToast({
								title:'账号不存在'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.login {
		.user-info {
			input {
				border: 1px solid #007AFF;
			}
		}
		.title {
			display: flex;
			justify-content: center;
		}
	}
</style>
